/**
 * 
 */
package com.swim.natatoriumorder.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.swim.natatoriumgoods.entity.NatatoriumGoods;
import com.swim.natatoriumorder.entity.NatatoriumOrder;
import com.swim.natatoriumorder.entity.OrderStudent;
import com.swim.natatoriumorder.service.NatatoriumOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 游泳馆订单Controller
 * @author tql
 * @version 2021-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/natatoriumorder/natatoriumOrder")
public class NatatoriumOrderController extends BaseController {

	@Autowired
	private NatatoriumOrderService natatoriumOrderService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public NatatoriumOrder get(String id, boolean isNewRecord) {
		NatatoriumOrder result = natatoriumOrderService.get(id, isNewRecord);
		return result;
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(NatatoriumOrder natatoriumOrder, Model model) {
		model.addAttribute("natatoriumOrder", natatoriumOrder);
		return "swim/natatoriumorder/natatoriumOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<NatatoriumOrder> listData(NatatoriumOrder natatoriumOrder, HttpServletRequest request, HttpServletResponse response) {
		Page<NatatoriumOrder> page = natatoriumOrderService.findPage(new Page<NatatoriumOrder>(request, response), natatoriumOrder);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = "form")
	public String form(NatatoriumOrder natatoriumOrder, Model model) {
		model.addAttribute("natatoriumOrder", natatoriumOrder);
		return "swim/natatoriumorder/natatoriumOrderForm";
	}

	/**
	 * 保存游泳馆订单
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated NatatoriumOrder natatoriumOrder) {
		return renderResult(Global.TRUE, text("保存订单成功！"), natatoriumOrderService.saveAuto(natatoriumOrder));
	}
	
//	
//	/**
//	 * 保存游泳馆订单
//	 */
//	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
//	@PostMapping(value = "save")
//	@ResponseBody
//	public String save(@Validated NatatoriumOrder natatoriumOrder) {
//		return renderResult(Global.TRUE, text("保存订单成功！"), natatoriumOrderService.saveAuto(natatoriumOrder));
//	}
	
	/**
	 * 删除游泳馆订单
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(NatatoriumOrder natatoriumOrder) {
		natatoriumOrderService.delete(natatoriumOrder);
		return renderResult(Global.TRUE, text("删除游泳馆订单成功！"));
	}
	
	/**
	 * 跳转查看详情页
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = "details")
	public String details(NatatoriumOrder natatoriumOrder, Model model) {
		model.addAttribute("natatoriumOrder", natatoriumOrder);
		return "swim/natatoriumorder/natatoriumorderDetails";
	}
	
	/**
	 * 获取订单学员数据
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = "creditRatingEventListData")
	@ResponseBody
	public String creditRatingEventListData(String natatoriumOrderId) {
		return renderResult(Global.TRUE, text("获取订单学员数据成功！"), natatoriumOrderService.orderStudentListData(natatoriumOrderId));
	}
	
	/**
	 * @param 订单号
	 * 获取详情数据
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String orderNum) {
        NatatoriumOrder natatoriumOrder = new NatatoriumOrder();
        natatoriumOrder.setOrderNum(orderNum);
        List<NatatoriumOrder> natatoriumOrder1 = natatoriumOrderService.findList(natatoriumOrder);
		return renderResult(Global.TRUE, text("获取详情数据成功！"), natatoriumOrderService.get(natatoriumOrder1.get(0).getId()));
	}
	
//	/**
//	 * 更新订单支付状态为已付款
//	 * @param natatoriumOrderId  订单id
//	 */
//	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
//	@RequestMapping(value = "updateOrderStatus")
//	@ResponseBody
//	public String updateOrderStatus(String natatoriumOrderId) {
//		natatoriumOrderService.updateOrderStatus(natatoriumOrderId, 2L);// 订单状态 1表示付款中 2表示已付款 3表示已核销 4表示已关闭
//		return renderResult(Global.TRUE, text("更新订单支付状态为已付款成功！"));
//	}

	/**
	 * 核销订单
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
	@PostMapping(value = "verification")
	@ResponseBody
	public String verification(String natatoriumOrderId) {
		NatatoriumOrder natatoriumOrder = natatoriumOrderService.get(natatoriumOrderId);
		if (natatoriumOrder != null) {
			// 商品类型 1表示游泳票 2表示培训课程
			if (natatoriumOrder.getNatatoriumGoods().getGoodsType() == 2L) {//培训课程
				//课程结束3天后才能核销订单
				Calendar c = Calendar.getInstance();
			    c.setTime(natatoriumOrder.getNatatoriumGoods().getCourseEndTime());
			    c.add(Calendar.HOUR_OF_DAY, 3);
			    Date verificationDate = c.getTime();
		        if(new Date().before(verificationDate)) {//当前时间早于课程结束时间+3天
		        	return renderResult(Global.TRUE, text("核销失败！课程结束3天后才能核销订单！"));
		        }  
		        natatoriumOrder.setSettlementStatus(4L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
			} else if (natatoriumOrder.getNatatoriumGoods().getGoodsType() == 1L) {//游泳票
				natatoriumOrder.setSettlementStatus(1L);		// 结算状态 1表示可结算(专指游泳票) 2表示不可结算 3表示可结算首款 4表示可结算尾款 5表示已结算首款 6表示已结算
			}
			natatoriumOrder.setOrderStatus(3L);		// 订单状态 1表示付款中 2表示已付款 3表示已核销 4表示已关闭
			natatoriumOrder.setVerifyPerson(UserUtils.getUser().getUserName());		// 核算人名
			natatoriumOrder.setVerifyDate(new Date());		// 核算时间
			natatoriumOrderService.update(natatoriumOrder);
		} else {
			return renderResult(Global.TRUE, text("查无此订单，核销失败！"));
		}
		return renderResult(Global.TRUE, text("核销成功！"));
	}
	
	//#################################################
	//################ 平台 游泳馆订单列表 ###################
	//#################################################
	
	/**
	 * 查询列表（平台 游泳馆订单列表）
	 */
//	@RequiresPermissions("natatoriumorder:natatoriumOrder:view")
	@RequestMapping(value = {"platformList", ""})
	public String platformList(NatatoriumOrder natatoriumOrder, Model model) {
		model.addAttribute("natatoriumOrder", natatoriumOrder);
		return "swim/natatoriumorder/natatoriumOrderPlatformList";
	}
	
}